package com.lune.job.controller;


import com.lune.job.framework.redis.RedisUtil;
import com.lune.job.pojo.Article;
import com.lune.job.pojo.ResPageBean;
import com.lune.job.pojo.RespBean;
import com.lune.job.service.IArticleService;
import com.lune.job.utils.UserThreadLocal;
import com.lune.job.vo.UserData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Caiyilin
 * @since 2022-03-06
 */
@RestController
@RequestMapping("/article")
@Api(tags = "文章管理")
public class ArticleController {

    @Autowired
    private IArticleService articleService;
    @Autowired
    private RedisUtil redisUtil;

    @PostMapping("/")
    @ApiOperation(value = "添加文章")
    public RespBean create(@RequestBody Article article){
        String token = UserThreadLocal.get();
        UserData userData= (UserData) redisUtil.get(token);
        article.setCreate_date(new Date());
        article.setCreate_user(userData.getId());
        boolean flag = articleService.save(article);
        if (flag){
            return RespBean.success("添加文章成功");
        }else{
            return RespBean.error("添加文章失败");
        }

    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除文章")
    public RespBean delete(@PathVariable Integer id){
        if (articleService.removeById(id)){
            return RespBean.success("删除文章成功");
        }else {
            return RespBean.error("删除文章失败");
        }
    }

    @DeleteMapping("/")
    @ApiOperation(value = "批量删除文章")
    public RespBean deleteByIds(Integer[]  ids){
        if (articleService.removeByIds(Arrays.asList(ids))){
            return RespBean.success("批量删除文章成功");
        }else {
            return RespBean.error("批量删除文章失败");
        }
    }

    @PutMapping("/")
    @ApiOperation(value = "修改文章")
    public RespBean update(@RequestBody Article article){
        if (articleService.updateById(article)){
            return RespBean.success("修改文章成功");
        }else {
            return RespBean.error("修改文章失败");
        }
    }

    @GetMapping("/detail")
    @ApiOperation(value = "根据id查询文章")
    public RespBean detail(Integer id){
        Article byId = articleService.getById(id);
        if (byId!=null){
            return RespBean.success("查询成功",byId);
        }else {
            return RespBean.error("查询失败");
        }
    }

    @GetMapping("/queryPage")
    @ApiOperation(value = "分页查询文章")
    public ResPageBean queryPage(@RequestParam(defaultValue = "1") Integer currentPage,
                                 @RequestParam(defaultValue = "10") Integer size,
                                 Article article){
        return articleService.queryPage(currentPage,size,article);
    }

    @PostMapping("/queryByCon")
    @ApiOperation(value = "条件查询文章")
    public List<Article> queryByCon(@RequestBody Article article){
        return articleService.queryByCon(article);
    }



}
